package cate.game.server.controller;

import cate.game.server.service.SystemService;
import cate.game.server.service.UsefulCtx;
import cp.solution.ops.status.SysOprStatus;
import cp.solution.pattern.Feedback;
import easy.java.practice.errorcode.EcException;
import easy.java.spring.annotation.MyGetMapping;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Slf4j
@Controller
@RequestMapping("system/")
public class SystemController extends GameSrvBaseController {

    @Resource
    UsefulCtx uc;
    @Resource
    SystemService service;

    @MyGetMapping("shutdown")
    @ResponseBody
    public Feedback shutdown(@RequestParam String secret) {
        log.info("收到关闭游戏进程の请求：secret={}", secret);
        if(!StringUtils.equals(secret, uc.getSecret())) {
            throw new EcException("secret不合法");
        }
        return service.shutdown();
    }

    @MyGetMapping("stopGame")
    @ResponseBody
    public Feedback stopGame(@RequestParam String secret, @RequestParam int srvId) {
        log.info("收到关闭GameBodyの请求：secret={} srvId={}", secret, srvId);
        if(!StringUtils.equals(secret, uc.getSecret())) {
            throw new EcException("secret不合法");
        }
        return service.stopGame(srvId);
    }

    @MyGetMapping("startGame")
    @ResponseBody
    public Feedback startGame(@RequestParam String secret, @RequestParam int srvId) throws Exception {
        log.info("收到启动GameBodyの请求：secret={} srvId={}", secret, srvId);
        if(!StringUtils.equals(secret, uc.getSecret())) {
            throw new EcException("secret不合法");
        }
        return service.startGame(srvId);
    }

    @MyGetMapping("opr/status")
    @ResponseBody
    public SysOprStatus loadSysOprStatus(@RequestParam String secret, @RequestParam int srvId) {
        log.debug("收到GMT的loadSysOprStatusの请求：secret={} srvId={}", secret, srvId);
        if(!StringUtils.equals(secret, uc.getSecret())) {
            throw new EcException("secret 不合法， 请检查GMT中的环境配置中的secret");
        }
        return service.loadSysOprStatus(srvId);
    }

    @MyGetMapping("merge/settle")
    @ResponseBody
    public SysOprStatus mergeSettle(@RequestParam String secret, @RequestParam int srvId) {
        log.info("收到GMT进行合服结算的の请求：secret={} srvId={}", secret, srvId);
        if(!StringUtils.equals(secret, uc.getSecret())) {
            throw new EcException("secret 不合法， 请检查GMT中的环境配置中的secret");
        }
        return service.mergeSettle(srvId);
    }

    @MyGetMapping("clean/data")
    @ResponseBody
    public SysOprStatus cleanData(@RequestParam String secret, @RequestParam int srvId) {
        log.info("收到GMT进行数据清理的の请求：secret={} srvId={}", secret, srvId);
        if(!StringUtils.equals(secret, uc.getSecret())) {
            throw new EcException("secret 不合法， 请检查GMT中的环境配置中的secret");
        }
        return service.cleanData(srvId);
    }


}
